package com.augurit.gzcgwsp.sendNotice.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.syslog.tool.SysTraceLogWriter;
import com.augurit.gzcgwsp.sendNotice.service.IAcceptNoticeService;
import com.augurit.wf.bi.web.form.WfBusInstanceTaskForm;
//TODO
//户外广告部分两个字段还没有设置好，1：申请日期   2：联系人
@Service
@Transactional
public class AcceptNoticeServiceImpl implements IAcceptNoticeService {
	private static final Logger logger = Logger.getLogger(CreateNoticeCommonPartServiceImpl.class);

	//--------------------------------------------------------------------------公共部分
	
	/**
	 * 公共方法 map
	 * @param sqrq				申请日期
	 * @param acceptNo			受理号
	 * @param cbyw				承办业务
	 * @param lxr				联系人
	 * @param sjr				收件人
	 * @param sqdw				申请单位
	 * @param task				WfBusInstanceTaskForm
	 * @return map
	 */
	public Map<String,String> getCommonAcceptNoticeMap(Date sqrq,String acceptNo,String cbyw,String lxr,String sjr,
			String sqdw,WfBusInstanceTaskForm task){
		Map<String, String> acceptNoticeMap = new HashMap<String, String>();
		try {			
			Date date = sqrq;//申请日期
			Date today = new Date();//受理日期
			
			String slh = "";//受理号
			String slhTemp = acceptNo;
			if(StringUtils.isNotEmpty(slhTemp)){
				slh = slhTemp;
			}
			
			String sxlx = cbyw;//事项类型
			
			String sqrqStr = "";//申请日期
			String sqrqTemp = sqrq.toString();
			if(StringUtils.isNotEmpty(sqrqTemp)){
				sqrqStr = sqrqTemp;
			}
			String sqrmc = "";//申请人名称
			String sqrmcTemp = lxr;
			if(StringUtils.isNotEmpty(sqrmcTemp)){
				sqrmc = sqrmcTemp;
			}
			String sary = "";//收案人
			String saryTemp = sjr;
			if(StringUtils.isNotEmpty(saryTemp)){
				sary = saryTemp;
			}
			String sqdwStr = "";
			String sqdwTemp = sqdw;
			if(StringUtils.isNotEmpty(sqdwTemp)){
				sqdwStr = sqdwTemp;
			}
			
			String year = Integer.toString((date.getYear()+1900));//申请--年
			String month = Integer.toString((date.getMonth()+1));//申请--月
			String day = Integer.toString(date.getDate());//申请--日
			String nian = Integer.toString((today.getYear()+1900));//受理--年
			String yue = Integer.toString((today.getMonth()+1));//受理--月
			String ri = Integer.toString(today.getDate());//受理日
			String lxdh = "";//
			String wlcx = "";//
			acceptNoticeMap.put("slh", slh);
			acceptNoticeMap.put("sxlx", sxlx);
			acceptNoticeMap.put("sqrmc", sqrmc);
			acceptNoticeMap.put("sqrq", sqrqStr);
			acceptNoticeMap.put("sary", sary);
			acceptNoticeMap.put("sqdw", sqdwStr);
			acceptNoticeMap.put("year", year);
			acceptNoticeMap.put("month", month);
			acceptNoticeMap.put("day", day);
			acceptNoticeMap.put("nian", nian);
			acceptNoticeMap.put("yue", yue);
			acceptNoticeMap.put("ri", ri);
			acceptNoticeMap.put("lxdh", lxdh);
			acceptNoticeMap.put("wlcx", wlcx);
		} catch (RuntimeException e) {
			logger.error(acceptNo+"产生acceptNoticeMap异常"+SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
			e.printStackTrace();
		}
		
		return acceptNoticeMap;
	}
	
	/**
	 * 产生受理通知书内容，xml格式
	 * @param 通知书内容的标签对应值map
	 * @return 
	 */
	public String createAcceptNotice(Map<String,String> map){
		Document document = DocumentHelper.createDocument();
		//�
		Element notice = document.addElement("notice");
		//受理号�
		Element slh = notice.addElement("slh");
		slh.addText(map.get("slh"));
		//事项类型
		Element sxlx = notice.addElement("sxlx");
		sxlx.addText(map.get("sxlx"));
		//申请人名称
		Element sqrmc = notice.addElement("sqrmc");
		sqrmc.addText(map.get("sqrmc"));
		//申请日期
		Element sqrq = notice.addElement("sqrq");//
		sqrq.addText(map.get("sqrq"));
		//收案人员
		Element sary = notice.addElement("sary");
		sary.addText(map.get("sary"));
		//申请单位
		Element sqdw = notice.addElement("sqdw");
		sqdw.addText(map.get("sqdw"));//
		//申请--年
		Element year = notice.addElement("year");
		year.addText(map.get("year"));
		//申请--月
		Element month = notice.addElement("month");
		month.addText(map.get("month"));
		//申请--日
		Element day = notice.addElement("day");
		day.addText(map.get("day"));
		//受理--年
		Element nian = notice.addElement("nian");
		nian.addText(map.get("nian"));
		//受理--月
		Element yue = notice.addElement("yue");
		yue.addText(map.get("yue"));//��������-��

		Element ri = notice.addElement("ri");
		ri.addText(map.get("ri"));//��������-��

		Element lxdh = notice.addElement("lxdh");
		lxdh.addText(map.get("lxdh"));
		
		Element wlcx = notice.addElement("wlcx");
		wlcx.addText(map.get("wlcx"));

		String str =  document.asXML();
		String noticeStr = str.substring(str.lastIndexOf("?>") + 2).trim();//
		
		noticeStr = noticeStr.replace("<", "&lt;");
		noticeStr = noticeStr.replace(">", "&gt;");
		noticeStr = noticeStr.replace("&", "&amp;");
		noticeStr = noticeStr.replace("‘", "&apos;");
		noticeStr = noticeStr.replace("\"", "&quot;");
		return noticeStr;
	}
	
}
